package com.raimcomputing.pickforme.common.utility;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class VoFactory {
	private static Log log = LogFactory.getLog( VoFactory.class );

	/**
	 * Returns an instantiated VO of the specified class.
	 * 
	 * @param clazz
	 *            The class of VO to return.
	 * @return An instantiated VO of the specified class.
	 */
	public static Object getVo( Class clazz ) {
		String temp = clazz.getName() + "Impl";
		String nameToLoad = temp.substring( 0, temp.lastIndexOf( "." ) );
		nameToLoad += ".impl"
				+ temp.substring( temp.lastIndexOf( "." ), temp.length() );

		log.debug( "Class requested: '" + clazz.getName() + "'. Loading: '"
				+ nameToLoad + "'." );

		Class resultClass;
		Object result;
		try {
			resultClass = Class.forName( nameToLoad );
			result = resultClass.newInstance();
		} catch ( ClassNotFoundException e ) {
			log.error( "Requested class '" + clazz.getName()
					+ "' is unresolvable.", e );
			throw new IllegalArgumentException();
		} catch ( InstantiationException e ) {
			log.error( "Unable to instantiate class", e );
			throw new IllegalArgumentException();
		} catch ( IllegalAccessException e ) {
			log.error( "Unable to access class", e );
			throw new IllegalArgumentException();
		}

		return (result);
	}

}
